package logic

import (
	"context"
	"fmt"
	"month/model/mysql"

	"month/shoprpc/internal/svc"
	"month/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductsAddLogic {
	return &ProductsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductsAddLogic) ProductsAdd(in *shoprpc.ProductsAddRequest) (*shoprpc.ProductsAddResponse, error) {
	// todo: add your logic here and delete this line
	products := mysql.Products{
		Num:    in.Num,
		Name:   in.Name,
		Price:  float64(in.Price),
		Money:  float64(in.Money),
		Image:  in.Image,
		UserId: int(in.UserId),
	}

	err := mysql.DB.Debug().Model(&mysql.Products{}).Create(&products).Error
	if err != nil {
		return nil, fmt.Errorf("发表失败")
	}
	return &shoprpc.ProductsAddResponse{Id: int64(products.ID)}, nil
}
